package com.blueocean_health.care.management.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.ReserveFunds;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.ReserveFundsDto;
import com.blueocean_health.care.management.domain.dto.ReserveFundsDvo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.mapper.FundsTypeMapper;
import com.blueocean_health.care.management.mapper.ReserveFundsMapper;
import com.blueocean_health.care.management.mapper.UserMapper;
import com.blueocean_health.care.management.service.ReserveFundsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ReserveFundsServiceImpl implements ReserveFundsService{
	
	@Autowired
	private ReserveFundsMapper fundsDao;
	
	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private FundsTypeMapper typeDao;
	
	DecimalFormat df = new DecimalFormat("0.00");
	
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	/**
	 * 根据筛选条件获取所有备用金信息  2017年12月6日19:17:36
	 */
	@Override
	public PageInfo<ReserveFundsDto> getAllReserveFunds(ReserveFundsDvo fundsDvo) {
		User user1 = LocalManager.getVal(LocalManagerType.USER_WEB);
//		map.put("hospitalId", user1.getHospitalId());
		fundsDvo.setHospitalId(user1.getHospitalId());
//		int pageSize = Integer.parseInt(map.get("pageSize")+"");
//		int page = Integer.parseInt(map.get("page")+"");
		//根据负责人进行筛选
		if(StringUtils.isEmpty(fundsDvo.getManager())){
			fundsDvo.setManager("");
//			map.put("manager", "");
		}else{
			String userId = fundsDvo.getManager();
			User user = userDao.getUserByUserId(userId);
			String userRealName = user.getUserRealName();
			fundsDvo.setManager(userRealName);
//			map.put("manager", userRealName);
		}
		
		String[] fee = fundsDvo.getReserveFeeType();
		if(fee.length <= 0 || StringUtils.isEmpty(fee)){
			fundsDvo.setReserveFeeType(null);
		}else if(fee.length == 1){
			fee[0] = fee[0]+",";
			fundsDvo.setReserveFeeType(fee);
		}
		PageHelper.startPage(fundsDvo.getPage(),fundsDvo.getPageSize());
		List<ReserveFundsDto> allReserveFunds = fundsDao.getAllReserveFunds(fundsDvo);
		for (ReserveFundsDto reserveFunds : allReserveFunds) {
			if("1".equals(reserveFunds.getAccountsType())){
				reserveFunds.setExpend(df.format(reserveFunds.getAccountsFee()));
				reserveFunds.setIncome(null);
			}else if("2".equals(reserveFunds.getAccountsType())){
				reserveFunds.setIncome(df.format(reserveFunds.getAccountsFee()));
				reserveFunds.setExpend(null);
			}
		}
		PageInfo<ReserveFundsDto> pageInfo = new PageInfo<ReserveFundsDto>(allReserveFunds);
		return pageInfo;
	}
	/**
	 * 根据筛选条件获取所有备用金信息导出excel  2017年12月7日10:10:03
	 */
	@Override
	public List<ReserveFundsDto> getReserveFundsExcel(Map<String,String> map) {
		User user1 = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user1.getHospitalId());
		//根据负责人进行筛选
		if(StringUtils.isEmpty(map.get("manager"))){
			map.put("manager", "");
		}else{
			String userId = map.get("manager")+"";
			User user = userDao.getUserByUserId(userId);
			String userRealName = user.getUserRealName();
			map.put("manager", userRealName);
		}
		Object object =  map.get("reserveFeeType");
		String string = object.toString();
		String[] split = string.split(",");
		if("[]".equals(string) || StringUtils.isEmpty(string)){
			map.put("reserveFeeType",null);
		}else if(split.length == 1){
			map.put("reserveFeeType",split[0]+",");
		}
		List<ReserveFundsDto> allReserveFunds = fundsDao.getExcelReserveFunds(map);
		for (ReserveFundsDto reserveFunds : allReserveFunds) {
			if("1".equals(reserveFunds.getAccountsType())){
				reserveFunds.setExpend(df.format(reserveFunds.getAccountsFee()));
				reserveFunds.setIncome(null);
			}else if("2".equals(reserveFunds.getAccountsType())){
				reserveFunds.setIncome(df.format(reserveFunds.getAccountsFee()));
				reserveFunds.setExpend(null);
			}
		}
		return allReserveFunds;
	}

	/**
	 * 备用金管理  信息保存  2017年12月6日16:21:36
	 */
	@Override
	public int insertReserveFunds(ReserveFunds reserveFunds) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		reserveFunds.setHospitalId(user.getHospitalId());
		Long random = (long)(Math.random()*500)+100;
		try {
			Thread.sleep(random);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		reserveFunds.setUpdateTime(sdf.format(new Date()));
		System.out.println("-----"+reserveFunds.getUpdateTime());
		//获取备用金费用类别名称
		String[] reserveFeeType = reserveFunds.getReserveFeeType();
		String feeName = "";
		if(reserveFeeType.length < 2 || StringUtils.isEmpty(reserveFeeType[1])){
			if(reserveFeeType.length == 1){
				reserveFeeType[0] = reserveFeeType[0]+",";
				reserveFunds.setReserveFeeType(reserveFeeType);
			}
			feeName = typeDao.selectFeeNameById(reserveFeeType[0]);
			
		}else{
			String feeNameById = typeDao.selectFeeNameById(reserveFeeType[0]);
			String feeNameById2 = typeDao.selectFeeNameById(reserveFeeType[1]);
			feeName = feeNameById+"-"+feeNameById2;
		}
		reserveFunds.setReserveFeeName(feeName);
		int funds = fundsDao.insertReserveFunds(reserveFunds);
		return funds;
	}

	@Override
	public int updateReserveFundsById(ReserveFunds reserveFunds) {
		//获取备用金费用类别名称
		String[] reserveFeeType = reserveFunds.getReserveFeeType();
		reserveFunds.setUpdateTime(sdf.format(new Date()));
		String feeName = "";
		if(reserveFeeType.length < 2 || StringUtils.isEmpty(reserveFeeType[1])){
			if(reserveFeeType.length == 1){
				reserveFeeType[0] = reserveFeeType[0]+",";
				reserveFunds.setReserveFeeType(reserveFeeType);
			}
			feeName = typeDao.selectFeeNameById(reserveFeeType[0]);
			
		}else{
			String feeNameById = typeDao.selectFeeNameById(reserveFeeType[0]);
			String feeNameById2 = typeDao.selectFeeNameById(reserveFeeType[1]);
			feeName = feeNameById+"-"+feeNameById2;
		}
		reserveFunds.setReserveFeeName(feeName);
		int fundsById = fundsDao.updateReserveFundsById(reserveFunds);
		return fundsById;
	}

	@Override
	public ReserveFunds getReserveFundsById(Integer id) {
		ReserveFunds reserveFundsById = fundsDao.getReserveFundsById(id);
		return reserveFundsById;
	}

	@Override
	public int deleteReserveFundsById(Integer id) {
		int delete = fundsDao.deleteReserveFundsById(id);
		return delete;
	}

}
